<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.attachmentName"
        placeholder="附件名称"
        style="width: 200px; margin-right: 10px"
        class="filter-item"
        @keyup.enter.native="handleSearch"
      />

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        新增
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      stripe
      fit
      highlight-current-row
      style="width: 100%"
      @sort-change="sortChange"
    >
      <el-table-column
        label="附件编号"
        prop="id"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="{ row }">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="附件名称" min-width="110px">
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{
            row.attachmentName
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" min-width="110px">
        <template slot-scope="{ row }">
          <span class="link-type" @click="handleUpdate(row)">{{
            row.description
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容类型" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.contentType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createdUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="附件路径" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.dir }}</span>
        </template>
      </el-table-column>
      <el-table-column label="附件大小" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.size }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            查看
          </el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            下载
          </el-button>

          <el-button
            v-if="row.status != 'deleted'"
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />

    <el-dialog title="新增附件" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="80px"
        style="width: 400px; margin-left: 50px"
      >
        <el-form-item label="附件key" prop="name">
          <el-input v-model="temp.key" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="createData()"> 提交 </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { fetchCargoList, createCargo, delCargo, getGoodsTypeList } from '@/api/tms/cargo'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        attachmentName: undefined
      },
      temp: {
        id: undefined,
        goodsTypeId: '',
        name: '',
        weight: '',
        volume: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑货物',
        create: '新增货物'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        goodsTypeId: [
          { required: true, message: '类型必须选', trigger: 'change' }
        ],
        name: [{ required: true, message: '标题必须填', trigger: 'blur' }]
      },
      goodsTypeList: []
    }
  },
  created() {
    this.getGoodsTypeList()
  },
  methods: {
    getGoodsTypeList() {
      getGoodsTypeList().then((response) => {
        this.goodsTypeList = response
        this.getList()
      })
    },
    goodsTypeName(id) {
      const data = this.goodsTypeList.filter(item => item.id === id)
      return data.length > 0 ? data[0].name || '' : ''
    },
    goodsTypeId(name) {
      const data = this.goodsTypeList.filter(item => item.name === name)
      return data.length > 0 ? data[0].id || '' : ''
    },
    getList() {
      this.listLoading = true
      fetchCargoList(this.listQuery).then((response) => {
        console.log(response)
        this.list = response.records
        this.total = response.total
        this.listLoading = false
      })
    },
    // 搜索
    handleSearch() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {},
    // 重置表单内容
    resetTemp() {
      this.temp = {
        id: undefined,
        goodsTypeId: '',
        name: '',
        weight: '',
        volume: ''
      }
    },
    // 新增弹窗
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 更新或者新增
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createCargo(this.temp).then((res) => {
            this.getList()
            this.dialogFormVisible = false
            const msg = this.dialogStatus === 'create' ? '添加成功' : '编辑成功'
            this.$notify({
              title: '成功',
              message: msg,
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    // 更新弹窗
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 处理删除选项
    handleDelete(row) {
      const arr = []
      arr.push(row.id)
      console.log(arr)
      delCargo(arr).then(() => {
        this.getList()
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
      })
    }
  }
}
</script>

<style lang="scss">
.filter-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  .align-items {
    margin-bottom: 0;
  }
}
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  button {
    width: 100px;
  }
}
</style>
